<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 餐桌分类
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$store_id=$this->session('store_id');
if(!$store_id){
	exi('请先选择门店！','error');
}
if($_RQ['table_id']){
		$table_id = intval($_RQ['table_id']);
		$this->session('table_id',$table_id);
}
if($_RQ['op']=='main'){
	$store=pdo_get('jy_dish_store',array('id'=>$store_id));
	$groups = pdo_getall('jy_dish_goods_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'));
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['store_id']=$store_id;
	if($_RQ['group_id']){
		$group_id = intval($_RQ['group_id']);
		$params['group_id']=$group_id;
	}else{
		$params['group_id']=$groups[0]['id'];
	}
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$carts=$this->session('carts');
	$allnum =0;
	if($carts){
		foreach($carts as $k=>&$v){
			$allnum += $v['num'];
		}
	}else{
		$carts = array();
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$total='';
	$fields=array('id','title','group_id','status','store_id','image','price','display','package','isoption');
	$list=pdo_getslice('jy_dish_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$g){
			$g['package']=$g['package']?json_decode($g['package'],true):array();
			if(!empty($g['package'])){
				foreach($g['package'] as $k=>&$p){
					$p['price']=$p['price']?$p['price']:$g['price'];
					$cartspackage = $carts[$g['id']]['package'];
					$p['num']=$cartspackage[$g['id']]['num'] ? $cartspackage[$g['id']]['num'] : 0;
					$g['cartnum'] +=$p['num'];
					$goodsprice += $p['num']*$p['price'];
				}	
			}else{
				$g['cartnum']= $carts[$g['id']]['num'] >0 ? $carts[$g['id']]['num'] : 0;
				$goodsprice += $g['cartnum']*$g['price'];
			}
		}
	}
	 //print_r($list);exit;
	$allprice = $goodsprice - $youhui;
	$return['list']=$list;
	$return['groups']=$groups;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['params']['group_id'] =$params['group_id'];
	$return['allnum']=$allnum;
	$return['allprice']=$allprice;
	$return['orderprice']=$allprice;
	$return['in_pay']=$store['in_pay'];
	$return['order_type']=$this->session('order_type');
	$return['carts']=$carts;
	$return['post']=array();
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='diancai'){
	$goodid=intval($_RQ['goodid']);
	$num=intval($_RQ['num']);
	$packageid=intval($_RQ['packageid']);
	$carts=$this->session('carts');
	if(!$carts){
		$carts=array();
	}
	if($num < 1){
		unset($carts[$goodid]);
	}else{
		$carts[$goodid]['num']=$num;
		$goodinfo=pdo_get('jy_dish_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$goodid));
		$carts[$goodid]['goodstitle'] = $goodinfo['title'];
		$carts[$goodid]['goodsprice'] = $goodinfo['price'];
		$packageinfo=json_decode($goodinfo['package'],true);
		$package=$carts[$goodid]['package'];
		if(!empty($packageinfo)){
			foreach($packageinfo as $k=>&$v){
				if($k == $packageid){
					$package[$k]['title']=$v['title'];
					$package[$k]['price']=$v['price']?$v['price']:$goodinfo['price'];
					if($_RQ['plus']){
						$package[$k]['num'] +=1;
					}else{
						$package[$k]['num'] -=1;
						if($package[$k]['num'] == 0){
							unset($package[$k]);
						}
					}
				}
			}
			$carts[$goodid]['package'] = $package;
		}
	}
	$carts=$this->session('carts',$carts);
	$return['carts'] = $this->session('carts');
	exi($return);
}
if($_RQ['op']=='unsetcarts'){
	$carts=$this->session('carts',array());
	$return['carts'] = $this->session('carts');
	exi($return);
}
if($_RQ['op']=='addorder'){
	$post=get_postdata();
	$store_id=$this->session('store_id');
	if(!$store_id){
		exi('请先选择门店！','error','jy_dish/index/');
	}	
	$store=pdo_get('jy_dish_store',array('id'=>$store_id));
	$youhui = 0;
	$carts=$this->session('carts');
	if(!$carts){
		exi('没有选择商品！','error');
	}
	$total=0;
	foreach($carts as $k=>&$v){
			$allnum += $v['num'];
			$goodinfo = pdo_get('jy_dish_goods',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id,'id'=>$k));
			$v['packageinfo']=$goodinfo['package']?json_decode($goodinfo['package'],true):array();
			if(!empty($v['packageinfo'])){
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i]['num'];
					if(empty($p['price'])){
						$p['price']=$goodinfo['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}
					$total += $p['num']*$p['price'];
				}	
			}else{
				$total += $v['num']*$goodinfo['price'];
			}
	}
	if($this->session('order_type')==1){
		$table=pdo_get('jy_dish_table',array('id'=>$this->session('table_id')));
		pdo_update('jy_dish_table',array('busy'=>2),array('uniacid'=>$_SESSION['uniacid'],'id'=>$this->session('table_id')));
		$price=$store['in_pay']*$post['user_num']+$total - $youhui;	
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>1,
			'ordersn'=>get_ordersn('jy_dish_order','ordersn'),
			'price'=>$price,
			'is_pay'=>$post['is_pay'],
			'remark'=>$post['remark'],
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_dish_order',$data);
		$this->session('insertid',pdo_insertid());
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'user_num'=>$post['user_num'],
			'user_fee'=>$store['in_pay'],
			'table_id'=>$this->session('table_id'),
			'order_id'=>pdo_insertid()
		);
		pdo_insert('jy_dish_order_table',$data);
	}
	if($this->session('order_type')==2){
		$lng=json_decode($store['lat_lng'],true);
		$out_distance=$store['out_distance'];
		$user_lng=$post['receiver_latng'];
		if($user_lng){
			$distance=cfc('tools')->getdistance($lng['lng'],$lng['lat'],$user_lng['lng'],$user_lng['lat']);
			if($distance>$out_distance){
				exi('超出配送距离','error');
			}
		}
		if($store['out_end']<$total){
			$price=$total - $youhui;
		}else{
			$price=$total+$store['out_pay'] -$youhui;	
		}
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'mid'=>$_SESSION['uid'],
			'order_type'=>2,
			'ordersn'=>get_ordersn('jy_dish_order','ordersn'),
			'price'=>$price,
			'is_pay'=>$post['is_pay'],
			'remark'=>$post['remark'],
			'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_dish_order',$data);
		$this->session('insertid',pdo_insertid());
		$addressinfo=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'isdefault'=>1));
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'order_id'=>pdo_insertid(),
			'delivery_fee'=>$store['out_pay'],
			'receiver_name'=>$post['name'],
			'receiver_mobile'=>$post['mobile'],
			'receiver_address'=>$post['address'],
			'receiver_latng'=>$post['lng_lat'],
			'out_time'=>ijson_encode($post['out_time']),
		);
		$addressinfo['pcd']=json_decode($addressinfo['pcd'],true);
		pdo_insert('jy_dish_order_out',$data);
	}
	if($this->session('order_type')==3){
		$price=$total - $youhui;
		$data=array(
		'uniacid'=>$_SESSION['uniacid'],
		'store_id'=>$store_id,
		'mid'=>$_SESSION['uid'],
		'order_type'=>3,
		'is_pay'=>$post['is_pay'],
		'ordersn'=>get_ordersn('jy_dish_order','ordersn'),
		'price'=>$price,
		'createtime'=>TIMESTAMP		
		);
		pdo_insert('jy_dish_order',$data);
		$this->session('insertid',pdo_insertid());
		$data=array(
			'uniacid'=>$_SESSION['uniacid'],
			'store_id'=>$store_id,
			'order_id'=>$this->session('insertid'),
			'username'=>$addressinfo['name'],
			'mobile'=>$addressinfo['mobile'],
			'selfnumber'=>$post['receiver_latng'],
			'selftime'=>ijson_encode($post['selftime']),
		);
		pdo_insert('jy_dish_order_self',$data);
		$id = pdo_insertid();
		$selfnumber=sprintf("%05d", $id);
		pdo_update('jy_dish_order_self',array('selfnumber'=>$selfnumber),array('id'=>$id));
	}
	$is_pays= array('0'=>'未支付','1'=>'已支付');
	$content1 = "名称　             数量    单价\n";
	foreach($carts as $k=>&$v){
			$str='';
			$good=pdo_get('jy_dish_goods',array('id'=>$k));
			$v['packageinfo']=$good['package']?json_decode($good['package'],true):array();
			if(!empty($v['packageinfo'])){
				$str .= $good['title']."--规格如下：\n";
				foreach($v['packageinfo'] as $i=>&$p){
					$p['num']=$v['package'][$i]['num'];
					if(empty($p['price'])){
						$p['price']=$good['price'];
					}
					if(empty($p['num'])){
						unset($v['packageinfo'][$i]);
					}else{
						$money = $p['price'] * $p['num'];
						$str .= '----' . str_pad($p['title'], '20', ' ', STR_PAD_RIGHT);
						$str .= ''.str_pad($p['num'], '5', ' ', STR_PAD_RIGHT);
						$str .= ''. number_format($money, 2) . "元\n";
					}
				}
			}else{
				$money = $good['price'] * $v['num'];
				$str .= ''.str_pad($good['title'], '25', ' ', STR_PAD_RIGHT);
				$str .= ''.str_pad($v['num'], '5', ' ', STR_PAD_RIGHT);
				$str .= ''. number_format($money, 2) . "元\n";
			}
			$content1 .=$str;
			$data=array(
				'uniacid'=>$_SESSION['uniacid'],
				'store_id'=>$store_id,
				'order_id'=>$this->session('insertid'),
				'goods_id'=>$k,
				'goods_num'=>$v['num'],
				'goods_price'=>$good['price'],			
				'goods_title'=>$good['title'],
				'goods_package'=>ijson_encode($v['packageinfo']),
			);
			pdo_insert('jy_dish_order_goods',$data);
	}
	$content1 .= "------------------\n";
	$this->session('carts','');
	$nickname=pdo_getcolumn('core_members',array('id'=>$_SESSION['uid']),'nickname');
	$order_typek = $this->session('order_type');
	$order_type=$this->type__order();
	$order_type=$order_type[$order_typek];
	$tpldata=array(
		'first'=>array('value'=>'订单下单通知','color'=>''),
		'tradeDateTime'=>array('value'=>'下单时间:'.date('Y-m-d h:i:s',TIMESTAMP),'color'=>'#ff510'),
		'orderType'=>array('value'=>'订单类型:'.$order_type,'color'=>'#ff510'),
		'customerInfo'=>array('value'=>'下单人'.$nickname,'color'=>'#ff510'),
		'orderItemName'=>array('value'=>'菜品详情','color'=>'#ff510'),
		'orderItemData'=>array('value'=>'菜品详情内容\n'.$content1,'color'=>'#ff510'),
		'remark'=>array('value'=>'其他信息','color'=>'#ff510'),
	);
	$url=custom_url('jy_dish/order_show/',true);
	$text = '';
	if($order['order_type'] == 3){
		$text .='取餐号:'.$selfinfo['selfnumber']."\n";
	}
	$text .='下单时间:'.date('Y-m-d h:i:s',TIMESTAMP)."\n";
	$text .='订单类型:'.$order_type."\n";
	$text .="是否支付:".$is_pays[$post['is_pay']]."\n";
	$text .="下单人:代客下单\n";
	$text .="【菜品详情】\n";
	$text .="------------------\n";
	$text .=$content1."\n";
	$text .='用户名:'.$post['name']."\n";
	$text .='手机号码:'.$post['mobile']."\n";
	//发信息
	//$this->notice('order_new',$text,$tpldata,$_SESSION['uid'],$url,'#980000');
	//打印
	$this->printmsg($text,$this->session('insertid'),0,1);
	exi('提交成功');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择餐桌！','error');
	}
	$update=array();
	$post=get_postdata();
	if(isset($post['status'])){
		$update['status']=intval($post['status']);
	}
	if($update){
		pdo_update('jy_dish_table',$update,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_dish_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		
	}else{
		$item=array();
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回餐桌类型列表'),
		),
		'edits'=>array(
			array(
				'title'=>'基本信息',
				'list'=>array(
					array('type'=>'string','title'=>'餐桌编号','name'=>'title','help'=>''),
					array('type'=>'select','title'=>'餐桌类型','name'=>'group_id','help'=>'','options'=>'groups'),
					array('type'=>'image','title'=>'餐桌图片','name'=>'image','help'=>''),
					array('type'=>'switch','title'=>'是否启用','name'=>'status','help'=>''),
				)
			),
		)
	);
	$return['groups']=pdo_getall('jy_dish_table_group',array('uniacid'=>$_SESSION['uniacid'],'store_id'=>$store_id),array('id','title'),'id');
	$return['htmls']=$htmls;
	set_html('framess',$this->menustore());
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$data=array(
		'title'=>trim($postdata['title']),
		'group_id'=>intval($postdata['group_id']),
		'status'=>intval($postdata['status']),
		'image'=>trim($postdata['image']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_dish_table',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['store_id']=$store_id;
		pdo_insert('jy_dish_table',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_dish_table',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}